package com.project.platform.controller;


import com.project.platform.entity.User;
import com.project.platform.service.UserService;
import com.project.platform.utils.CurrentUserThreadLocal;
import com.project.platform.vo.PageVO;
import com.project.platform.vo.ResponseVO;
import jakarta.annotation.Resource;
import org.apache.coyote.Response;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("list")
    public ResponseVO<List<User>> list(){
        return ResponseVO.ok(userService.list());//Response返回的是前后端约定的规格化数据
    }

    @GetMapping("page")
    public ResponseVO<PageVO<User>> page(@RequestParam Map<String,Object> query ,@RequestParam(defaultValue = "1") Integer pageNo,@RequestParam(defaultValue = "10") Integer pageSize){
        PageVO<User> page = userService.page(query, pageNo, pageSize);
        return ResponseVO.ok(page);
    }

    @GetMapping("selectById/{id}")
    public ResponseVO<User> selectById(@PathVariable("id") Integer id){
        return ResponseVO.ok(userService.selectById(id));
    }

    @GetMapping("selectByUsername/{username}")
    public ResponseVO<User> selectByUsername(@PathVariable("username") String username){
        return ResponseVO.ok(userService.selectByUsername(username));
    }

    @PostMapping("add")
    public ResponseVO add(@RequestBody User user){
        userService.insert(user);
        return ResponseVO.ok();
    }

    @PutMapping("update")
    public ResponseVO update(@RequestBody User user){
        userService.update(user);
        return ResponseVO.ok();
    }

    @DeleteMapping("delete")
    public ResponseVO delete(@RequestBody List<Integer> ids){
        userService.delete(ids);
        return ResponseVO.ok();
    }

    @PostMapping("/topUp/{amount}")
    public ResponseVO topUp(@PathVariable Float amount) {
        Integer userId = CurrentUserThreadLocal.getCurrentUser().getId();
        userService.topUp(userId, amount);
        return ResponseVO.ok();
    }

    @PutMapping("Account")
    public ResponseVO Account(@RequestBody User user) {
        userService.Account(user);
        return ResponseVO.ok();
    }

}
